package com.jader.fragment;
/*商品详情页*/
import java.util.ArrayList;

import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jader.activities.Seller;
import com.jader.activities.ShowGoodsBigPictrue;
import com.jader.activities.Store;
import com.jader.beans.Goods;
import com.jader.beans.Goods_Detail;
import com.jader.util.JaderApplication;
import com.lesogo.cu.custom.ScaleView.HackyViewPager;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class Seller_goods_fragment extends Fragment implements OnClickListener {

	private RequestQueue queue;
	private JaderApplication application;
	private ImageView evalute_user_picture;
	private Button more_evalute;
	private TextView seller_goods_title, seller_goods_ders, seller_sales,
			seller_address, seller_goods_price, seller_stock, evalute_username,
			evalute_user_time, evalute_user_content;
	private String url = JaderApplication.getIP() + "/Jader/client?type="
			+ JaderApplication.GOODS_DETAIL + "&userid=8&goodsid=";

	private JsonObjectRequest objectRequest;
	private Goods_Detail goods_detail;
	private com.jader.beans.Seller seller;
	private Goods goods;
	private Gson gson;
	private View view;
	private Seller activity;
	private int goodsid;
	private HackyViewPager viewPager;
	private ArrayList<View> allListView;
	private String picture;
	private CheckBox checkBox;
	private ImageView iv_back;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = (Seller) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.seller_goods_fragment_layout,
				container, false);
		initViews();
		return view;
	}

	@Override
	public void onResume() {
		sendReques(url + goods.getGoodsid());
		super.onResume();
	}

	private void initViews() {
		seller_goods_title = (TextView) view
				.findViewById(R.id.seller_goods_title);
		seller_goods_ders = (TextView) view
				.findViewById(R.id.seller_goods_ders);
		seller_sales = (TextView) view.findViewById(R.id.seller_sales);
		seller_address = (TextView) view.findViewById(R.id.seller_address);
		seller_stock = (TextView) view.findViewById(R.id.seller_stock);
		seller_goods_price = (TextView) view
				.findViewById(R.id.seller_goods_price);
		evalute_user_picture = (ImageView) view
				.findViewById(R.id.evalute_user_picture);
		evalute_user_content = (TextView) view
				.findViewById(R.id.evalute_user_content);
		evalute_user_time = (TextView) view
				.findViewById(R.id.evalute_user_time);
		iv_back = (ImageView) view.findViewById(R.id.iv_back);
		iv_back.setOnClickListener(activity);
		evalute_username = (TextView) view.findViewById(R.id.evalute_username);
		more_evalute = (Button) view.findViewById(R.id.seller_more_comment);
		checkBox = (CheckBox) view.findViewById(R.id.goods_sc);
		checkBox.setClickable(false);
		checkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (checkBox.isChecked()) {
					collect_goods();
				} else {
					cancel_collect_goods();
				}

			}
		});
		more_evalute.setOnClickListener(this);

		gson = new Gson();
		goods = (Goods) getActivity().getIntent().getSerializableExtra("goods");
		activity.getPopWindow().setGoods(goods);
		goodsid = goods.getGoodsid();
		application = (JaderApplication) getActivity().getApplication();
		queue = application.getQueue();

	}

	private void initData() {
		seller = goods_detail.getSeller();
		seller_address.setText(seller.getAddress().getCity() + "  发货");
		seller_goods_ders.setText(goods.getDesc());
		seller_goods_title.setText(goods.getGoodsName());
		seller_sales.setText("销量：" + goods.getNumber());
		seller_stock.setText("剩余：" + goods.getStock());
		seller_goods_price.setText("¥" + goods.getPrice());
		JaderApplication.getBitmapUtils().display(
				evalute_user_picture,
				JaderApplication.getIP()+"/Jader"
						+ goods_detail.getGoods_comment().getUser()
								.getPicture());
		evalute_user_content.setText(goods_detail.getGoods_comment()
				.getEvalute().getContent());
		String date = goods_detail.getGoods_comment().getEvalute().getTime();
		if (date == null) {
			evalute_user_time.setText("暂无用户评论");
			evalute_user_picture.setVisibility(View.GONE);
			more_evalute.setVisibility(View.GONE);
		} else {
			evalute_user_time.setText("发表于" + date);
		}
		evalute_username.setText(goods_detail.getGoods_comment().getUser()
				.getUsername());
		checkBox.setChecked(goods_detail.isCollection());
		initViewPager();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.seller_more_comment:
			activity.openEvalute(goodsid);
			break;
		case R.id.enter_into:
			Intent intent = new Intent(activity, Store.class);
			int sellerid = goods.getSellerid();
			intent.putExtra("sellerid", sellerid);
			startActivity(intent);
			break;

		case R.id.lianxi_lefu:
			Intent phone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ seller.getPhone()));
			startActivity(phone);
			break;
		default:
			break;
		}
	}

	private void sendReques(String url) {
		objectRequest = new JsonObjectRequest(url, null,
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						goods_detail = gson.fromJson(response.toString(),
								Goods_Detail.class);
						initData();
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
					}
				});
		queue.add(objectRequest);
	}

	private void initViewPager() {
		picture = goods.getPicture();
		if (allListView != null) {
			allListView.clear();
			allListView = null;
		}
		allListView = new ArrayList<View>();
		View view = LayoutInflater.from(activity).inflate(R.layout.g_pic_item,
				null);
		ImageView imageView = (ImageView) view.findViewById(R.id.g_pic_item);
		imageView.setScaleType(ScaleType.FIT_XY);
		JaderApplication.getBitmapUtils().display(imageView,
				JaderApplication.getIP()+"/Jader"+ goods.getPicture());
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(activity, ShowGoodsBigPictrue.class);
				intent.putExtra("picture", picture);
				startActivity(intent);
			}
		});
		allListView.add(view);
		viewPager = (HackyViewPager) Seller_goods_fragment.this.view
				.findViewById(R.id.iv_baby);
		ViewPagerAdapter adapter = new ViewPagerAdapter();
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		viewPager.setAdapter(adapter);

	}

	private class ViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return allListView.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (View) arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = allListView.get(position);
			container.addView(view);
			return view;
		}

	}

	private void collect_goods() {
		if (JaderApplication.user == null) {
			checkBox.setChecked(false);
			JaderApplication.login(activity);
		} else {
			String url = JaderApplication.getIP() + "/Jader/client?type="
					+ JaderApplication.COLLECTID_GOODS + "&userid="
					+ JaderApplication.user.getUserid() + "&goodsid=" + goodsid;
			HttpUtils httpUtils = new HttpUtils();
			httpUtils.configCurrentHttpCacheExpiry(10);
			httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					checkBox.setChecked(false);
					JaderApplication.show(activity, "访问网络失败");

				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					String result = arg0.result;
					boolean flag = false;
					if (result != null && !"".equals(result)) {
						flag = Boolean.parseBoolean(result);
					}
					if (flag) {
						checkBox.setChecked(flag);
					} else {
						checkBox.setChecked(!flag);
						JaderApplication.show(activity, "操作失败，请重试");
					}
				}
			});
		}
	}

	private void cancel_collect_goods() {
		if (JaderApplication.user == null) {
			checkBox.setChecked(false);
			JaderApplication.login(activity);
		} else {
			String url = JaderApplication.getIP() + "/Jader/client?type="
					+ JaderApplication.CANCEL_COLLECTID_GOODS + "&userid="
					+ JaderApplication.user.getUserid() + "&goodsid=" + goodsid;
			HttpUtils httpUtils = new HttpUtils();
			httpUtils.configCurrentHttpCacheExpiry(10);
			httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					checkBox.setChecked(true);
					JaderApplication.show(activity, "访问网络失败");

				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					String result = arg0.result;
					boolean flag = false;
					if (result != null && !"".equals(result)) {
						flag = Boolean.parseBoolean(result);
					}
					if (flag) {
						checkBox.setChecked(!flag);
					} else {
						checkBox.setChecked(flag);
						JaderApplication.show(activity, "操作失败，请重试");
					}
				}
			});
		}
	}

	public void setBackgroundBlack(View view, int what) {
		switch (what) {
		case 0:
			view.setVisibility(View.VISIBLE);
			break;
		case 1:
			view.setVisibility(View.GONE);
			break;
		}
	}
}
